package com.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.model.Logging;
import com.model.Staff;
import com.model.User;
import com.service.ILoggingService;
import com.service.IUserService;
import com.utils.MeiJiaUtils;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private IUserService userService;
	@Resource
	private ILoggingService loggingService;
	private int page = 20;//每页分20条	
	@RequestMapping("/showUser")
	public String showUser(User user, String pages,HttpServletRequest request) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("cardno", user.getCardno());
		map.put("name", user.getName());
		map.put("phone", user.getPhone());
		map.put("idcard", user.getIdcard());
		
		int curPage = 0;
		if (pages == null || pages.trim().equals("")) {
			curPage = 1;
		} else {
			curPage = Integer.parseInt(pages);
		}
		request.setAttribute("curPage", curPage);
		map.put("min", (curPage - 1) * page);
		map.put("max", page);
		List<User> u = userService.findAll(map);
		int count =userService.countUsers(map);
		if (count % page == 0) {
			request.setAttribute("pageNumber", count / page);
		} else {
			request.setAttribute("pageNumber", count / page + 1);
		}
		request.setAttribute("userList", u);
		request.setAttribute("user", user);
		request.setAttribute("count", count);
		return "showUser";
	}
	
	@RequestMapping("/saveUser")
	public String saveUser(User u,String creatTime,HttpServletRequest request) throws ParseException{
		List<User> list = userService.findUserByNo(u.getCardno());
		if(list.size() >0){
			request.getSession().setAttribute("msg", MeiJiaUtils.setMessage("该卡号已经存在!"));
		}else{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			u.setStatus(1);
			u.setDate(sdf.parse(creatTime));
			userService.saveUser(u);
			Logging log = new Logging();
			log.setCardno(u.getCardno());
			log.setDate(new Date());
			log.setMoney(u.getMoney());
			log.setName(u.getName());
			log.setStatus("开卡金额");
			log.setOperatePerson(((Staff)request.getSession().getAttribute("sessionStaff")).getName());
			loggingService.saveLogging(log);
			
		}
		return "redirect:/user/showUser.action";
	}
	
	@RequestMapping("/saveUserForm")
	public String saveUserForm(HttpServletRequest request){
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = formatter.format(date);
		request.setAttribute("currentDate",currentDate);
		return "saveUserForm";
	}
	
	@RequestMapping("/indexForm")
	public String indexForm(){
		return "showUser";
	}
	
	@RequestMapping("/updateUser")
	public String updateUser(Integer id,String pages,double price,HttpServletRequest request){
		double oldPrice = price;
		User u = userService.getUserById(id);
		Logging l = new Logging();
		price = u.getMoney() + price;
		if(price < 0){
			request.getSession().setAttribute("msg", MeiJiaUtils.setMessage("余额不足!"));
		}else{
			u.setMoney(price);
			u.setStatus(0);
			userService.updateUser(u);
		}
		if(oldPrice<0)
			l.setStatus("消费");
		else
			l.setStatus("冲值");
		Staff staff = (Staff)request.getSession().getAttribute("sessionStaff");
		l.setName(u.getName());  // 会员名称
		l.setMoney(Math.abs(oldPrice));
		l.setCardno(u.getCardno());
		l.setDate(new Date());
		l.setOperatePerson(staff.getName());
		loggingService.saveLogging(l);
		return "redirect:/user/showUser.action?pages=" + Integer.parseInt(pages);
	}
	
	@RequestMapping("/updateUserForm")
	public String updateUserForm(int id,HttpServletRequest request){
		User u = userService.getUserById(id);
		request.setAttribute("user", u);
		return "updateUser";
	}
	
	@RequestMapping("/updateUserbyId")
	public String updateUserbyId(User u,HttpServletRequest request){
		userService.updateUser(u);
		return "redirect:/user/showUser.action";
	}
	
	@RequestMapping("/userDetail")
	public String findUserbyId(int id,HttpServletRequest request){
		User u = userService.getUserById(id);
		request.setAttribute("user", u);
		return "userDetail";
	}
	
	@RequestMapping("/findUserByNo")
	public String findUserByNo(String cardno,String pages,HttpServletRequest request){		
		Map<Object, Object> map = new HashMap<Object, Object>();
		int curPage = 0;
		if (pages == null || pages.trim().equals("")) {
			curPage = 1;
		} else {
			curPage = Integer.parseInt(pages);
		}
		request.setAttribute("curPage", curPage);
		map.put("min", (curPage - 1) * page);
		map.put("max", page);
		List<User> u = userService.findUserByNo(cardno);
		int count =userService.countUserByNo(cardno);
		if (count % page == 0) {
			request.setAttribute("pageNumber", count / page);
		} else {
			request.setAttribute("pageNumber", count / page + 1);
		}
		request.setAttribute("user", u);
		return "showUser";
	}
	
	@RequestMapping("/lishi")
	public String lishi(String cardno,String name,String pages,HttpServletRequest request) throws Exception{
		Map<Object, Object> map = new HashMap<Object, Object>();
		int curPage = 0;
		List<Logging> loggings= loggingService.findLoggingByNo(cardno);
		if(loggings != null && loggings.size()>0){
			if (pages == null || pages.trim().equals("")) {
				curPage = 1;
			} else {
				curPage = Integer.parseInt(pages);
			}
			request.setAttribute("curPage", curPage);
			map.put("min", (curPage - 1) * page);
			map.put("max", page);
			map.put("cardno", cardno);
			if(!cardno.isEmpty()){
				request.setAttribute("cardno", cardno);
			}
			List<Logging> l = loggingService.findUserBypage(map);
			int count =loggingService.countLoggingByNo(cardno);
			if (count % page == 0) {
				request.setAttribute("pageNumber", count / page);
			} else {
				request.setAttribute("pageNumber", count / page + 1);
			}
			request.setAttribute("loggings", l);
			request.setAttribute("count", count);
			
		}else{
			request.setAttribute("loggings", null);
		}
		request.setAttribute("cardno", cardno);
		request.setAttribute("name", name);
		return "lishi";
	}
	
	@RequestMapping("/alllog")
	public String getAllLogging(String pages,HttpServletRequest request){
		Map<Object, Object> map = new HashMap<Object, Object>();
		int curPage = 0;
		if (pages == null || pages.trim().equals("")) {
			curPage = 1;
		} else {
			curPage = Integer.parseInt(pages);
		}
		request.setAttribute("curPage", curPage);
		map.put("min", (curPage - 1) * page);
		map.put("max", page);
		List<Logging> logs = loggingService.getAllLogging(map);
		int count = loggingService.countAllLogging();
		if (count % page == 0) {
			request.setAttribute("pageNumber", count / page);
		} else {
			request.setAttribute("pageNumber", count / page + 1);
		}
		request.setAttribute("logs", logs);
		return "alllog";
	}
	
	/**
	 * 余额清零 modified By xiaochao
	 */
	@RequestMapping("/cancelUser")
	public String cancelUser(Integer id,HttpServletRequest request){
		User u = userService.getUserById(id);
		Logging log = new Logging();
		log.setCardno(u.getCardno());
		log.setName(u.getName());
		log.setDate(new Date());
		log.setMoney(u.getMoney());
		log.setOperatePerson(MeiJiaUtils.getSessionStaff(request).getName());
		log.setStatus("余额清零");
		loggingService.saveLogging(log);
		u.setMoney(0.00);
		u.setStatus(1);
		userService.updateUser(u);
		return "redirect:/user/showUser.action";
	}
	
	@RequestMapping("/checkNo")
	@ResponseBody
	public Map<String, Object> checkNo(String cardno,HttpServletRequest request){
		List<User> list = userService.findUserByNo(cardno);
		Map<String, Object> map = new HashMap<String, Object>();
		if(list.size() >0){
			map.put("success", false);
		}else{
			map.put("success", true);
		}
		
		return map;
	}
	
}
